package com.wisezone.permission.service.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.wisezone.common.exception.WzServiceException;
import com.wisezone.common.service.impl.BaseServiceImpl;
import com.wisezone.permission.bean.entity.WzRescEntity;
import com.wisezone.permission.bean.entity.WzUserEntity;
import com.wisezone.permission.bean.entity.WzUserGroupEntity;
import com.wisezone.permission.bean.entity.WzUserRoleEntity;
import com.wisezone.permission.bean.search.WzUserSearch;
import com.wisezone.permission.dao.WzUserDao;
import com.wisezone.permission.service.WzUserGroupService;
import com.wisezone.permission.service.WzUserRoleService;
import com.wisezone.permission.service.WzUserService;

/**
 * 
 *用户; InnoDB free: 24576 kB
 * 
 * @author create by create date 2010-07-29 14:44:49 update date 2010-07-29
 *         14:44:49
 * 
 */
public class WzUserServiceImpl extends BaseServiceImpl implements WzUserService {

	/**
	  *
	  *
	  *
	  */
	private WzUserDao wzUserDao;

	private WzUserGroupService wzUserGroupService;

	private WzUserRoleService wzUserRoleService;
	


	/**
	 * 登录
	 * 
	 * @param loginName
	 * @param password
	 * @return
	 * @throws WzServiceException
	 */
	public WzUserEntity login(String loginName, String password)
			throws WzServiceException {

		if (StringUtils.isEmpty(loginName) || StringUtils.isEmpty(password)) {
			throw new WzServiceException("用户名或密码参数不能为空");
		}
		WzUserSearch searchEntity = new WzUserSearch();
		searchEntity.setLoginName(loginName);
		searchEntity.setPassword(password);
		try {
			return wzUserDao.get(searchEntity);
		} catch (Exception e) {
			throw new WzServiceException("获得用户出错");
		}

	}

	/**
	 * 保存或修改用户
	 * 
	 * @param entity
	 * @param resIds
	 * @throws WzServiceException
	 */
	public void saveOrUpdate(WzUserEntity entity, String roleIds,
			String groupIds) throws WzServiceException {

		// 保存用户
		this.saveOrUpdate(entity);

		// 删除用户角色
		this.wzUserRoleService.deleteByUserId(entity.getId());

		// 删除用户用户组
		this.wzUserGroupService.deleteByUserId(entity.getId());

		// 保存用户角色
		if (StringUtils.isNotEmpty(roleIds)) {
			String roleIdStr[] = roleIds.split(",");
			for (String roleId : roleIdStr) {

				WzUserRoleEntity userRoleEntity = new WzUserRoleEntity();
				userRoleEntity.setRoleId(Long.valueOf(roleId));
				userRoleEntity.setUserId(entity.getId());
				wzUserRoleService.save(userRoleEntity);
			}
		}
		// 保存用户用户组
		if (StringUtils.isNotEmpty(groupIds)) {
			String groupIdStr[] = groupIds.split(",");
			for (String groupId : groupIdStr) {

				WzUserGroupEntity userGroupEntity = new WzUserGroupEntity();
				userGroupEntity.setGroupId(Long.valueOf(groupId));
				userGroupEntity.setUserId(entity.getId());
				wzUserGroupService.save(userGroupEntity);
			}
		}
	}

	/**
	 * 删除用户
	 * 
	 * @param idList
	 * @throws WzServiceException
	 */
	public void delete(Collection<Serializable> idList)
			throws WzServiceException {

		// 空就返回
		if (idList == null || idList.size() == 0) {
			return;
		}

		// 删除用户角色
		for (Iterator<Serializable> it = idList.iterator(); it.hasNext();) {
			Long id = Long.valueOf(it.next().toString());
			this.wzUserRoleService.deleteByUserId(id);
		}

		// 删除用户用户组
		for (Iterator<Serializable> it = idList.iterator(); it.hasNext();) {
			Long id = Long.valueOf(it.next().toString());
			this.wzUserGroupService.deleteByUserId(id);
		}

		// 删除用户
		this.deleteAll(idList);
	}

	public void setWzUserDao(WzUserDao wzUserDao) {
		this.setBaseDao(wzUserDao);
		this.wzUserDao = wzUserDao;
	}

	public void setWzUserGroupService(WzUserGroupService wzUserGroupService) {
		this.wzUserGroupService = wzUserGroupService;
	}

	public void setWzUserRoleService(WzUserRoleService wzUserRoleService) {
		this.wzUserRoleService = wzUserRoleService;
	}
	

}